"""
问题2调试脚本 - 检查功率计算过程
"""

import numpy as np
from problem2_solver_enhanced import Problem2SolverEnhanced

def debug_power_calculation():
    """调试功率计算过程"""
    print("="*60)
    print("🔍 问题2功率计算调试")
    print("="*60)
    
    # 创建求解器
    solver = Problem2SolverEnhanced()
    
    # 测试参数
    xT, yT, w, h, hm = 0, 0, 6, 4, 4
    r_rings = [120, 150, 180, 220, 260, 300]
    
    print(f"📊 测试参数:")
    print(f"   吸收塔位置: ({xT}, {yT})")
    print(f"   定日镜尺寸: {w} × {h} m")
    print(f"   安装高度: {hm} m")
    print(f"   环半径: {r_rings}")
    print(f"   DNI平均值: {solver.dni_avg} W/m²")
    
    # 生成布局
    print("\n🔄 生成布局...")
    positions = solver.generate_layout(xT, yT, w, h, hm, r_rings)
    print(f"✅ 生成 {len(positions)} 个定日镜位置")
    
    # 单镜面积
    mirror_area = w * h
    print(f"   单镜面积: {mirror_area} m²")
    print(f"   总镜面积: {len(positions) * mirror_area:,.0f} m²")
    
    # 详细计算一个时刻的功率
    print("\n🔄 详细计算过程...")
    alpha_s, gamma_s = solver.solar_positions[0]  # 第一个时刻
    print(f"   太阳位置: α={alpha_s:.2f}°, γ={gamma_s:.2f}°")
    
    total_efficiency = 0.0
    total_power = 0.0
    valid_mirrors = 0
    
    for i, (xi, yi) in enumerate(positions[:10]):  # 只检查前10个镜子
        eta = solver._calculate_single_mirror_efficiency(
            xi, yi, xT, yT, solver.tower_height, hm, w, h, alpha_s, gamma_s
        )
        power_mirror = solver.dni_avg * w * h * eta / 1e6  # MW
        
        if i < 5:  # 显示前5个的详细信息
            distance = np.sqrt((xi - xT)**2 + (yi - yT)**2)
            print(f"   镜子{i+1}: 位置=({xi:.1f}, {yi:.1f}), 距离={distance:.1f}m, 效率={eta:.4f}, 功率={power_mirror*1000:.1f}kW")
        
        total_efficiency += eta
        total_power += power_mirror
        if eta > 0.01:  # 有效镜子
            valid_mirrors += 1
    
    print(f"\n📈 前10个镜子统计:")
    print(f"   有效镜子数: {valid_mirrors}/10")
    print(f"   平均效率: {total_efficiency/10:.4f}")
    print(f"   总功率: {total_power:.4f} MW")
    print(f"   单镜平均功率: {total_power/10*1000:.1f} kW")
    
    # 估算全场功率
    estimated_total = total_power / 10 * len(positions)
    print(f"\n🎯 全场估算:")
    print(f"   估算总功率: {estimated_total:.2f} MW")
    print(f"   理论最大功率: {len(positions) * mirror_area * solver.dni_avg / 1e6:.2f} MW (100%效率)")
    
    # 使用求解器的方法计算
    print("\n🔄 使用求解器方法计算...")
    power, efficiency = solver.calculate_power_efficiency(positions, xT, yT, w, h, hm)
    print(f"✅ 求解器结果:")
    print(f"   功率: {power:.2f} MW")
    print(f"   效率: {efficiency:.4f}")
    
    # 约束检查
    print("\n🔄 约束检查...")
    feasible, power_check, efficiency_check = solver._check_constraints(xT, yT, w, h, hm, positions)
    print(f"✅ 约束检查结果:")
    print(f"   可行性: {'✅' if feasible else '❌'}")
    print(f"   功率: {power_check:.2f} MW")
    print(f"   效率: {efficiency_check:.4f}")
    print(f"   是否满足60MW: {'✅' if power_check >= 60 else '❌'}")

if __name__ == "__main__":
    debug_power_calculation() 